HTMLify
Maximum Sum Problem.java
Views: 1 | Author: cody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | // Maximum Sum Problem import java.io.*; import java.util.*; class GfG { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); Solution ob = new Solution(); System.out.println(ob.maxSum(n)); } } } // } Driver Code Ends //User function Template for Java class Solution { public int maxSum(int n) { Map<Integer, Integer> memo = new HashMap<>(); return maxSumDivide(n, memo); } private int maxSumDivide(int n, Map<Integer, Integer> memo) { if(n==0){ return 0; } if(memo.containsKey(n)==true){ return memo.get(n); } int maxS=Math.max(n,maxSumDivide(n/2,memo)+maxSumDivide(n/3,memo)+maxSumDivide(n/4,memo)); memo.put(n,maxS); return maxS; } } |